package top.lyunz.lucky.hr.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import top.lyunz.lucky.hr.bean.Config_file_first_kind;
import top.lyunz.lucky.hr.bean.Config_file_second_kind;
import top.lyunz.lucky.hr.bean.Config_file_third_kind;
import top.lyunz.lucky.hr.bean.Config_major;
import top.lyunz.lucky.hr.bean.Config_major_kind;
import top.lyunz.lucky.hr.bean.HumanFile;
import top.lyunz.lucky.hr.bean.Salary_standard_details;
import top.lyunz.lucky.hr.service.Config_file_first_kindService;
import top.lyunz.lucky.hr.service.Config_file_second_kindService;
import top.lyunz.lucky.hr.service.Config_file_third_kindService;
import top.lyunz.lucky.hr.service.Config_majorService;
import top.lyunz.lucky.hr.service.Config_major_kindService;
import top.lyunz.lucky.hr.service.HumanFileService;
import top.lyunz.lucky.hr.service.Salary_standard_detailsService;
import top.lyunz.lucky.hr.util.simpleResult;

@Controller
@RequestMapping("/human")
public class humanResourceController {

	@Autowired
	private Config_file_first_kindService	config_file_first_kindServicet;
	@Autowired
	private Config_file_second_kindService	config_file_second_kindService;
	@Autowired
	private Config_file_third_kindService	config_file_third_kindService;
	@Autowired
	private Config_major_kindService config_major_kindService;
	@Autowired
	private Config_majorService config_majorService;
	@Autowired
	private Salary_standard_detailsService salary_standard_detailsService;
	@Autowired
	private HumanFileService humanFileService;
	/*人力资源档案登记*/
	@RequestMapping("/human_register")
	public String human_register(Model model) {
		
		List<Config_file_first_kind> first = config_file_first_kindServicet.getAll();
		
		List<Config_major_kind> majorKind = config_major_kindService.getAll();
		
		List<Salary_standard_details> salary = salary_standard_detailsService.getAll();
		
		model.addAttribute("salary", salary);
		model.addAttribute("first", first);
		model.addAttribute("majorKind", majorKind);
		
		return "page/humanResources/human_register";
	}
	/*II级机构*/
	@RequestMapping("/human_kind/{id}")
	public @ResponseBody simpleResult human_second(@PathVariable("id") int id) {
		
		 List<Config_file_second_kind> second = config_file_second_kindService.getbyid(id);
		
		return new simpleResult(second);
	} 
	/*III级机构*/
	@RequestMapping("/human_kind_third/{id}")
	public @ResponseBody simpleResult human_kind_third(@PathVariable("id") int id) {
		
		List<Config_file_third_kind> third = config_file_third_kindService.getbyid(id);
		
		return new simpleResult(third);
		
	}
	/*职位*/
	@RequestMapping("/human_majorName/{id}")
	public @ResponseBody simpleResult human_majorName(@PathVariable("id") int id) {
		
		List<Config_major> majorName = config_majorService.getbyid(id);
		
		return new simpleResult(majorName);
	}
	
	/*人力资源登录*/
	@RequestMapping("/humanFile")
	public String human_File(HumanFile humanFile,HttpSession session) {
		
		Config_file_first_kind first = config_file_first_kindServicet.getbyid(humanFile.getFirstKindId());
		
		Config_file_second_kind second = config_file_second_kindService.getonebyid(humanFile.getSecondKindId());
		
		Config_file_third_kind third = config_file_third_kindService.getonebyid(humanFile.getThirdKindId());
		
		Config_major_kind majorKind = config_major_kindService.getonebyid(humanFile.getHumanMajorKindId());
		
		Config_major major = config_majorService.getonebyid(humanFile.getHumanMajorId());
		
		String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
		String  seconds = new SimpleDateFormat("HHmmss").format(new Date());
		
		int human_id =Integer.valueOf(seconds+getTwo()) ;
		
		System.out.println(human_id);
		
		HumanFile h = new HumanFile();
		h.setAttachmentName(humanFile.getAttachmentName());
		h.setBonusAmount(humanFile.getBonusAmount());
		h.setChanger(humanFile.getChanger());
		h.setChecker(humanFile.getChecker());
		h.setCheckStatus(2);
		h.setCheckTime(humanFile.getChangeTime());
		h.setDeleteTime(humanFile.getDeleteTime());
		h.setDemandSalaraySum(humanFile.getDemandSalaraySum());
		h.setFileChangAmount(humanFile.getFileChangAmount());
		h.setFirstKindId(first.getFirstKindId());
		h.setFirstKindName(first.getFirstKindName());
		h.setHfdId(humanFile.getHfdId());
		h.setHumanAccount(humanFile.getHumanAccount());
		h.setHumanAddress(humanFile.getHumanAddress());
		h.setHumanAge(humanFile.getHumanAge());
		h.setHumanBank(humanFile.getHumanBank());
		h.setHumanBirthday(humanFile.getHumanBirthday());
		h.setHumanBirthplace(humanFile.getHumanBirthplace());
		h.setHumanEducatedDegree(humanFile.getHumanEducatedDegree());
		h.setHumanEducatedMajor(humanFile.getHumanEducatedMajor());
		h.setHumanEducatedYears(humanFile.getHumanEducatedYears());
		h.setHumanEmail(humanFile.getHumanEmail());
		h.setHumanFamilyMembership(humanFile.getHumanFamilyMembership());
		h.setHumanFileStatus(humanFile.getHumanFileStatus());
		h.setHumanHistroyRecords(humanFile.getHumanHistroyRecords());
		h.setHumanHobby(humanFile.getHumanHobby());
		h.setHumanId(human_id);
		h.setHumanIdCard(humanFile.getHumanIdCard());
		h.setHumanMajorId(major.getMajorId());
		h.setHumanMajorKindId(majorKind.getMajorKindId());
		h.setHumanMajorKindName(majorKind.getMajorKindName());
		h.setHumanMobilephone(humanFile.getHumanMobilephone());
		h.setHumanName(humanFile.getHumanName());
		h.setHumanNationality(humanFile.getHumanNationality());
		h.setHumanParty(humanFile.getHumanParty());
		h.setHumanPicture(humanFile.getHumanPicture());
		h.setHumanPostcode(humanFile.getHumanPostcode());
		h.setHumanProDesignation(humanFile.getHumanProDesignation());
		h.setHumanQq(humanFile.getHumanQq());
		h.setHumanRace(humanFile.getHumanRace());
		h.setHumanReligion(humanFile.getHumanReligion());
		h.setHumanSex(humanFile.getHumanSex());
		h.setHumanSocietySecurityId(humanFile.getHumanSocietySecurityId());
		h.setHumanSpeciality(humanFile.getHumanSpeciality());
		h.setHumanTelephone(humanFile.getHumanTelephone());
		h.setHunmaMajorName(major.getMajorName());
		h.setLastlyChangeTime(humanFile.getLastlyChangeTime());
		h.setMajorChangeAmount(humanFile.getMajorChangeAmount());
		h.setPaidSalarySum(humanFile.getPaidSalarySum());
		h.setRecoveryTime(humanFile.getRecoveryTime());
		h.setRegister(humanFile.getRegister());
		h.setRemark(humanFile.getRemark());
		h.setSalaryStandardId(humanFile.getSalaryStandardId());
		h.setSalaryStandardName(humanFile.getSalaryStandardName());
		h.setSalarySum(humanFile.getSalarySum());
		h.setSecondKindId(second.getSecondKindId());
		h.setSecondKindName(second.getSecondKindName());
		h.setThirdKindId(third.getThirdKindId());
		h.setThirdKindName(third.getThirdKindName());
		h.setTrainingAmount(humanFile.getTrainingAmount());
		
		
		
		

		if(humanFileService.Add(h)>0) {
			
			session.setAttribute("id", human_id);
			
			return"page/humanResources/register_choose_picture";
		}else {
			return"page/humanResources/human_register";
		}
		
		
	}
	
	
	
	
	
	
	@RequestMapping("/upload")
	public String up (@RequestParam MultipartFile[] images, HttpSession session) throws FileNotFoundException {
		
		File path = new File(ResourceUtils.getURL("classpath:").getPath());
		
		File upload = new File(path.getAbsolutePath(),"static/uploads/");
		
		System.out.println(upload);
		
		for (MultipartFile m : images) {
			
			String fileName = m.getOriginalFilename();
			//使用当前时间，来让文件名不重复
			String date = String.valueOf(System.currentTimeMillis());
			//创建File文件流  放入路径和文件名 比如 D/a.png
			fileName = date+fileName;
			File file = new File(upload+"/"+fileName);
			try {
				//将文件写入file中
				m.transferTo(file);
				//文件名继续存放在数据库中，之后使用文件可以取出图片
				//调用添加方法。。。。
				int id = (int)session.getAttribute("id");
				
				humanFileService.Addimages(fileName, id);
				
			}catch (Exception e) {
				e.printStackTrace();
			}
			
		}
				
		return "page/humanResources/success";
		
		//return "page/humanResources/register_choose_attachment";
	}
	
	
	
	
	public static String getTwo(){
		Random rad=new Random();
 
		String result  = rad.nextInt(100) +"";
 
		if(result.length()==1){
			result = "0" + result;
		}
		return result;
	}


	
}
